#!/usr/bin/env python
# http://www.biophp.org/minitools/reduce_protein_alphabet/
value_table = {
#       gravy  charge solvat  entrop  maxasa      reduced amino acid alphabets  hydrorank        one letter
'ALA': ( 1.8,   0.0,   0.42,   0.00,  106.0, 'T',  'P',  'A',  'A',  'A',  'A',    9,     ),           # A
'ARG': (-4.5,   1.0,  -1.37,  -1.88,  248.0, 'C',  'P',  'E',  'K',  'K',  'K',   15,     ),           # R
'ASN': (-3.5,   0.0,  -0.82,  -1.03,  157.0, 'D',  'P',  'E',  'E',  'E',  'N',   16,     ),           # N
'ASP': (-3.5,  -1.0,  -1.05,  -0.78,  163.0, 'C',  'P',  'E',  'E',  'E',  'D',   19,     ),           # D
'ASX': (-3.5,  -0.5,  -1.05,  -0.78,  160.0, None, 'P',  'E',  'E',  'E',  None, None,    ),# 1kp0.pdb # B
'CYS': ( 2.5,   0.0,   1.34,  -0.85,  135.0, 'T',  'H',  'L',  'L',  'C',  'C',    7,     ),           # C
'CYH': ( 2.5,   0.0,   1.34,  -0.85,  135.0, 'T',  'H',  'L',  'L',  'C',  'C',    7,     ),           # C
'GLN': (-3.5,   0.0,  -0.30,  -1.73,  198.0, 'D',  'P',  'E',  'E',  'E',  'Q',   17,     ),           # Q
'GLU': (-3.5,  -1.0,  -0.87,  -1.46,  194.0, 'C',  'P',  'E',  'E',  'E',  'E',   18,     ),           # E
'GLX': (-3.5,  -0.5,  -0.87,  -1.46,  196.0, None, 'P',  'E',  'E',  'E',  None, None,    ),           # Z
'GLY': (-0.4,   0.0,   0.00,   0.00,   84.0, 'T',  'P',  'A',  'A',  'G',  'G',   11,     ),           # G
'HIS': (-3.2,   0.0,   0.18,  -0.95,  184.0, 'R',  'P',  'E',  'H',  'H',  'H',   10,     ),           # H
'ILE': ( 4.5,   0.0,   2.46,  -0.76,  169.0, 'A',  'H',  'L',  'L',  'L',  'L',    1,     ),           # I
'LEU': ( 3.8,   0.0,   2.32,  -0.71,  164.0, 'A',  'H',  'L',  'L',  'L',  'L',    3,     ),           # L
'LYS': (-3.9,   1.0,  -1.35,  -1.89,  205.0, 'C',  'P',  'E',  'K',  'K',  'K',   20,     ),           # K
'MET': ( 1.9,   0.0,   1.68,  -1.46,  188.0, 'D',  'H',  'L',  'L',  'L',  'L',    5,     ),           # M
'PHE': ( 2.8,   0.0,   2.44,  -0.62,  197.0, 'R',  'H',  'F',  'F',  'F',  'F',    2,     ),           # F
'PRO': (-1.6,   0.0,   0.98,  -0.06,  136.0, 'D',  'P',  'A',  'P',  'P',  'P',   13,     ),           # P
'CPR': (-1.6,   0.0,   0.98,  -0.06,  136.0, 'D',  'P',  'A',  'P',  'P',  'P',   13,     ),           # P
'SER': (-0.8,   0.0,  -0.05,  -1.11,  130.0, 'T',  'P',  'A',  'S',  'S',  'S',   14,     ),           # S
'THR': (-0.7,   0.0,   0.35,  -1.08,  142.0, 'D',  'P',  'A',  'S',  'S',  'T',   12,     ),           # T
'TRP': (-0.9,   0.0,   3.07,  -0.99,  227.0, 'R',  'H',  'F',  'F',  'F',  'W',    6,     ),           # W
'TYR': (-1.3,   0.0,   1.31,  -1.13,  222.0, 'R',  'H',  'F',  'F',  'F',  'F',    8,     ),           # Y
'VAL': ( 4.2,   0.0,   1.66,  -0.43,  142.0, 'A',  'H',  'L',  'L',  'L',  'L',    4,     ),           # V
'UNK': ( 0.0,   0.0,   0.00,   0.00,  000.0, None, None, None, None, None, None, None,    ),           # X
'XAA': ( 0.0,   0.0,   0.00,   0.00,  000.0, None, None, None, None, None, None, None,    ),           # X
'SEM': ( 0.0,   0.0,   0.00,   0.00,  000.0, 'D',  'H',  'L',  'L',  'L',  'L',    5,     ),  # SeMet? # M
'MSE': ( 0.0,   0.0,   0.00,   0.00,  000.0, 'D',  'H',  'L',  'L',  'L',  'L',    5,     ),  # SeMet  # M
'SEC': ( 2.5,   0.0,   1.34,  -0.85,  135.0, 'T',  'H',  'L',  'L',  'C',  'C',    7,     )}  # SeCys  # U
                                              #Aliphatic, aRomatic, Charged, Tiny, Diverse
                                                    #Hydrphobic, hydroPhylic
                                                          #Murphy4    #Murphy10
                                                                #Murphy8    #Murphy15

atom_table = {
# this table list all standard sidechain atoms for a residue of a given name.
# 0idx - sidechain NOCS.
# 1idx - sidechain hydrogens.
'ALA': ([' CB '],),
'ARG': ([' NE ', ' CB ',  ' CG ', ' CD ', ' CZ ', ' NH1', ' NH2'],),
'ASN': ([' ND2', ' CB ',  ' CG ', ' OD1'],),
'ASP': ([' CB ',  ' CG ', ' OD1', ' OD2'],),
'ASX': ([' CB ',  ' CG ', ' XD1', ' XD2'],),
'CYS': ([' CB ',  ' SG '],),
'CYH': ([' CB ',  ' SG '],),
'GLN': ([' CB ',  ' CG ', ' CD ', ' NE2', ' OE1'],),
'GLU': ([' CB ',  ' CG ', ' CD ', ' OE2', ' OE1'],),
'GLX': ([' CB ',  ' CG ', ' CD ', ' XE2', ' XE1'],),
'GLY': ([],),
'HIS': ([' CD2', ' CB ',  ' CG ', ' CE1', ' ND1', ' NE2'],),
'ILE': ([' CB ',  ' CD ', ' CD1', ' CG1', ' CG2'],),
'LEU': ([' CB ',  ' CG ', ' CD1', ' CD2'],),
'LYS': ([' NZ ', ' CB ',  ' CG ', ' CE ', ' CD '],),
'MET': ([' CB ',  ' CG ', ' CE ', ' SD '],),
'PHE': ([' CD2', ' CB ',  ' CG ', ' CZ ', ' CD1', ' CE1', ' CE2'],),
'PRO': ([' CB ',  ' CG ', ' CD '],),
'CPR': ([' CB ',  ' CG ', ' CD '],), # cis-proline
'SER': ([' OG ', ' CB '],),
'THR': ([' CB ',  ' OG1', ' CG2'],),
'TRP': ([' CZ2', ' CZ3', ' CD1', ' CD2', ' CH2', ' CB ',  ' CG ', ' CE3', ' CE2', ' NE1'],),
'TYR': ([' CD2', ' OH ', ' CB ',  ' CG ', ' CZ ', ' CD1', ' CE1', ' CE2'],),
'VAL': ([' CB ',  ' CG1', ' CG2'],),
'UNK': ([],),
'XAA': ([],),
'SEM': ([' CB ',  ' CG ', ' CE ', ' SD ', 'SE  '],),
'MSE': ([' CB ',  ' CG ', ' CE ', ' SD ', 'SE  '],)}

AA_NAMES = value_table.keys()
AA_BACKBONE = [' N  ', ' CA ', ' O  ', ' C  ']
AA_TERMINUS = [' OXT']
AA_SIDECHAIN = dict([(key_, value_[0]) for (key_, value_) in atom_table.iteritems()])
AA_HYDROGENS = {}
AA_NAMES_STR = str(AA_NAMES).replace("', '",',').replace("']",")").replace("['","(")
AA_SIDECHAIN_ALL = list(set(reduce(lambda x,y: x + y, [a[0] for a in atom_table.values()])))

AA_GRAVY = dict([(key_, value_[0]) for (key_, value_) in value_table.iteritems()])
AA_CHARGE = dict([(key_, value_[1]) for (key_, value_) in value_table.iteritems()])
AA_SOLVATATION = dict([(key_, value_[2]) for (key_, value_) in value_table.iteritems()]) #opposite to hydrophobicity
AA_ENTROPY = dict([(key_, value_[3]) for (key_, value_) in value_table.iteritems()])
AA_ASA = dict([(key_, value_[4]) for (key_, value_) in value_table.iteritems()])
AA_5 = dict([(key_, value_[5]) for (key_, value_) in value_table.iteritems()])
AA_2 = dict([(key_, value_[6]) for (key_, value_) in value_table.iteritems()])
AA_4MURPHY = dict([(key_, value_[7]) for (key_, value_) in value_table.iteritems()])
AA_8MURPHY = dict([(key_, value_[8]) for (key_, value_) in value_table.iteritems()])
AA_10MURPHY = dict([(key_, value_[9]) for (key_, value_) in value_table.iteritems()])
AA_15MURPHY = dict([(key_, value_[10]) for (key_, value_) in value_table.iteritems()])
AA_POLARITY = dict([(key_, value_[11]) for (key_, value_) in value_table.iteritems()])

HOH_NAMES = ['H_HOH', 'H_WAT', 'H_DOH', 'H_HOD', 'H_DOD']

AA_3to1 = {
  'ALA':'A', 'VAL':'V', 'PHE':'F', 'PRO':'P', 'MET':'M',
  'ILE':'I', 'LEU':'L', 'ASP':'D', 'GLU':'E', 'LYS':'K',
  'ARG':'R', 'SER':'S', 'THR':'T', 'TYR':'Y', 'HIS':'H',
  'CYS':'C', 'ASN':'N', 'GLN':'Q', 'TRP':'W', 'GLY':'G',
  '2AS':'D', '3AH':'H', '5HP':'E', 'ACL':'R', 'AIB':'A',
  'ALM':'A', 'ALO':'T', 'ALY':'K', 'ARM':'R', 'ASA':'D',
  'ASB':'D', 'ASK':'D', 'ASL':'D', 'ASQ':'D', 'AYA':'A',
  'BCS':'C', 'BHD':'D', 'BMT':'T', 'BNN':'A', 'BUC':'C',
  'BUG':'L', 'C5C':'C', 'C6C':'C', 'CCS':'C', 'CEA':'C',
  'CHG':'A', 'CLE':'L', 'CME':'C', 'CSD':'A', 'CSO':'C',
  'CSP':'C', 'CSS':'C', 'CSW':'C', 'CXM':'M', 'CY1':'C',
  'CY3':'C', 'CYG':'C', 'CYM':'C', 'CYQ':'C', 'DAH':'F',
  'DAL':'A', 'DAR':'R', 'DAS':'D', 'DCY':'C', 'DGL':'E',
  'DGN':'Q', 'DHA':'A', 'DHI':'H', 'DIL':'I', 'DIV':'V',
  'DLE':'L', 'DLY':'K', 'DNP':'A', 'DPN':'F', 'DPR':'P',
  'DSN':'S', 'DSP':'D', 'DTH':'T', 'DTR':'W', 'DTY':'Y',
  'DVA':'V', 'EFC':'C', 'FLA':'A', 'FME':'M', 'GGL':'E',
  'GLZ':'G', 'GMA':'E', 'GSC':'G', 'HAC':'A', 'HAR':'R',
  'HIC':'H', 'HIP':'H', 'HMR':'R', 'HPQ':'F', 'HTR':'W',
  'HYP':'P', 'IIL':'I', 'IYR':'Y', 'KCX':'K', 'LLP':'K',
  'LLY':'K', 'LTR':'W', 'LYM':'K', 'LYZ':'K', 'MAA':'A',
  'MEN':'N', 'MHS':'H', 'MIS':'S', 'MLE':'L', 'MPQ':'G',
  'MSA':'G', 'MSE':'M', 'MVA':'V', 'NEM':'H', 'NEP':'H',
  'NLE':'L', 'NLN':'L', 'NLP':'L', 'NMC':'G', 'OAS':'S',
  'OCS':'C', 'OMT':'M', 'PAQ':'Y', 'PCA':'E', 'PEC':'C',
  'PHI':'F', 'PHL':'F', 'PR3':'C', 'PRR':'A', 'PTR':'Y',
  'SAC':'S', 'SAR':'G', 'SCH':'C', 'SCS':'C', 'SCY':'C',
  'SEL':'S', 'SEP':'S', 'SET':'S', 'SHC':'C', 'SHR':'K',
  'SOC':'C', 'STY':'Y', 'SVA':'S', 'TIH':'A', 'TPL':'W',
  'TPO':'T', 'TPQ':'A', 'TRG':'K', 'TRO':'W', 'TYB':'Y',
  'TYQ':'Y', 'TYS':'Y', 'TYY':'Y', 'AGM':'R', 'GL3':'G',
  'SMC':'C', 'ASX':'B', 'CGU':'E', 'CSX':'C', 'GLX':'Z',
  'UNK':'X', 'XAA':'X', 'CPR':'P', 'CYH':'C'
  }

NA_NAMES = (' DT', ' DA', ' DC', ' DG')